﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fintech.Utils;
using ZOVNP.Model;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    public partial class OrderEditControl : UserControl
    {
        private class NewCustomer
        {
            public static readonly NewCustomer Value;

            static NewCustomer()
            {
                Value = new NewCustomer();
            }
            private NewCustomer()
            {
                Text = "< Новый покупатель... >";
            }

            public readonly string Text;

            public override string ToString()
            {
                return Text;
            }
        }

        private IOrder _order;
        private Workspace _workspace;

        public OrderEditControl(IOrder order, Workspace workspace, bool readOnly = false)
        {
            InitializeComponent();

            _workspace = workspace;

            List<object> customers = new List<object>(order.Context.Query<ICustomer>());
            customers.Insert(0, NewCustomer.Value);
            cbxCustomer.DataSource = customers;
            Order = order;

            this.tbxNumber.ReadOnly = this.tbxDescription.ReadOnly = tbxDeliveryAddress.ReadOnly = readOnly;
            this.cbxCustomer.Enabled = !readOnly;
            this.dtpDeliveryDate.Enabled = !readOnly;
        }

        public IOrder Order
        {
            get { return _order; }
            set
            {
                _order = value;

                tbxNumber.Text = _order != null ? _order.Number : string.Empty;
                cbxCustomer.SelectedItem = _order != null ? _order.Customer : null;

                if (_order != null) dtpDeliveryDate.Value = _order.DeliveryDate != null ? (DateTime)_order.DeliveryDate : DateTime.Now.AddDays(1);
                else dtpDeliveryDate.Value = DateTime.Now.AddDays(1);

                tbxDescription.Text = _order != null ? _order.Description : string.Empty;
            }
        }

        private void tbxNumber_TextChanged(object sender, EventArgs e)
        {
            if (_order != null)
                _order.Number = tbxNumber.Text;
        }

        private void cbxCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cbx = (ComboBox) sender;
            if (_order != null)
            {
                if (cbx.SelectedItem is ICustomer)
                {
                    var customer = (ICustomer)cbxCustomer.SelectedItem;
                    _order.Customer = customer;
                    if (tbxDeliveryAddress.Text == string.Empty && customer.DeliveryAddress != string.Empty)
                        tbxDeliveryAddress.Text = customer.DeliveryAddress;
                }
                else if (cbx.SelectedItem is NewCustomer)
                {
                    MessageBox.Show("Создание нового покупателя.");
                }
            }
        }

        private void dtpDeliveryDate_ValueChanged(object sender, EventArgs e)
        {
            if (_order != null)
                _order.DeliveryDate = dtpDeliveryDate.Value;
        }

        private void tbxDescription_TextChanged(object sender, EventArgs e)
        {
            if (_order != null)
                _order.Description = tbxDescription.Text;
        }

        private void btnCustomerInfo_Click(object sender, EventArgs e)
        {
            if(cbxCustomer.SelectedItem != null)
            {
                Form frm = new Form();
                var customer = (ICustomer) cbxCustomer.SelectedItem;
                frm.Text = "Покупатель " + customer.ToString();
                frm.ShowIcon = false;
                CustomerEditControl cec = new CustomerEditControl(customer, true);
                cec.Dock = DockStyle.Fill;
                frm.Controls.Add(cec);
                frm.ShowDialog();
            }
        }
    }
}
